package cn.stronglink.assetmanage.business.patrol.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.Patrol;
import cn.stronglink.assetmanage.listener.onRecyclerListener;

public class PatrolListWaitAdapter extends RecyclerView.Adapter<PatrolListWaitAdapter.ViewHolder>{

    private onRecyclerListener mListener;
    private Context mContext;
    private List<Patrol> mPagrolList;

    private LayoutInflater mLayoutInflater;
    public PatrolListWaitAdapter(Context context, List<Patrol> patrolList){

        this.mContext = context;
        this.mPagrolList = patrolList;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    public void setListener(onRecyclerListener listener){
        this.mListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_patrol_list_wait, parent, false);
        PatrolListWaitAdapter.ViewHolder viewHolder = new PatrolListWaitAdapter.ViewHolder(view,mListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Patrol patrol = mPagrolList.get(position);
        holder.bindBean(patrol);
        if (position%2!=0){
            holder.item_patrol.setBackgroundColor(mContext.getResources().getColor(R.color.gray_check_item));
        }else {
            holder.item_patrol.setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public int getItemCount() {
        return mPagrolList == null ? 0 : mPagrolList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView tv_patrolNo,tv_patrolName, tv_patrol_create_time,
            item_patrol_create_person,item_patrol_plan_time,item_patrol_execute_person;

        private onRecyclerListener mListener;
        private LinearLayout item_patrol;
        public ViewHolder(View itemView, onRecyclerListener listener) {
            super(itemView);

            tv_patrolNo = itemView.findViewById(R.id.item_patrol_wait_no);
            tv_patrolName = itemView.findViewById(R.id.item_patrol_wait_name);
            tv_patrol_create_time = itemView.findViewById(R.id.item_patrol_create_time);
            item_patrol_create_person = itemView.findViewById(R.id.item_patrol_create_person);
            item_patrol_plan_time = itemView.findViewById(R.id.item_patrol_plan_time);
            item_patrol_execute_person = itemView.findViewById(R.id.item_patrol_execute_person);
            item_patrol = itemView.findViewById(R.id.item_patrol);
            itemView.setOnClickListener(this);
            this.mListener = listener;

        }

        public void bindBean(Patrol patrol){
            tv_patrolNo.setText(patrol.getId_());
            //实体类待改
            tv_patrolName.setText(patrol.getName());
            tv_patrol_create_time.setText(patrol.getCreateName());
            item_patrol_create_person.setText(patrol.getOperatiorName());
            item_patrol_plan_time.setText(patrol.getBeginTime());
            item_patrol_execute_person.setText(patrol.getOperatiorName());
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClicked(v, getAdapterPosition());
            }
        }
    }

    public void notifyDatasChanged(List<Patrol> data) {
        this.mPagrolList = data;
        this.notifyDataSetChanged();
    }
}
